# -*- coding: utf-8 -*-//div[@class='mc']
from copy import deepcopy
import re
import scrapy


class JdSpider(scrapy.Spider):
    name = 'jd'
    allowed_domains = ['jd.com']
    start_urls = ['https://book.jd.com/booksort.html']

    def parse(self, response):
        # print(response.body.decode())
        dt_list = response.xpath('//div[@class="mc"]/dl/dt[1]')  # 大分类列表
        for dt in dt_list:
            item = {}
            item['b_cate'] = dt.xpath('./a/text()').extract_first()
            em_list = dt.xpath('./following-sibling::dd[1]/em')  # 小分类列表
            for em in em_list:
                item['s_href'] = em.xpath('./a/@href').extract_first()
                item['s_cart'] = em.xpath('./a/text()').extract_first()
                if item['s_href'] is not None:
                    item['s_href'] = 'https:' + item['s_href']
                    yield scrapy.Request(
                        item['s_href'],
                        callback=self.parse_book_list,
                        meta={'item': deepcopy(item)},
                    )

    def parse_book_list(self, response):  # 解析列表页
        # print('代码' + response.body.decode())
        webcode = response.body.decode()
        print(re.findall(r'SEARCH.base_url="(.*)";', webcode))
        print(re.findall(r'SEARCH.adv_param={(.*)};', webcode))
        print(re.findall(r'page:(.*),', webcode))
        item = response.meta['item']
        li_list = response.xpath('//div[@id="J_goodsList"]/ul/li[1]')
        for li in li_list:
            item['book_img'] = li.xpath('.//div[@class="p-img"]//img/@src').extract_first()
            if item['book_img'] is None:
                item['book_img'] = li.xpath('.//div[@class="p-img"]//img/@data-lazy-img').extract_first()
            item['book_name'] = li.xpath('.//div[@class="p-name"]//em/text()').extract_first()
            print(item)

